package  org.supda.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.supda.system.controller.vo.MenuVO;
import org.supda.system.dao.ResourceDao;
import org.supda.system.dao.UserDao;
import org.supda.system.entity.TButton;
import org.supda.system.entity.TResources;
import org.supda.system.entity.TUser;
import org.supda.system.service.ButtonService;
import org.supda.system.service.ResourceService;
import org.supda.system.service.UserService;
import org.supda.system.util.CommonUtils;
import org.supda.system.util.DataTablePageUtil;

@Service("ResourceService")
@Transactional
public class ResourceServiceImpl implements ResourceService{

	@Resource ResourceDao resourceDao;
	@Resource UserService userService; 
	@Resource ButtonService buttonService;
	
	@Override
	public List<TResources> listResources() {
		return resourceDao.find("From TResources");
	}

	@Override
	public TResources createResource(TResources resources) {
		return resourceDao.get(TResources.class, (Long)resourceDao.save(resources));
	}

	@Override
	public void deleteResource(Long resourcesId) {
		resourceDao.delete(resourceDao.get(TResources.class, resourcesId));
	}

	@Override
	public Map<TResources , List<TResources>> warpMenu(Set<TResources> resList) {
		
		Map<TResources , List<TResources>> resultMap = new HashMap<TResources, List<TResources>>();
		
		for(TResources tResources : resList){
			if(tResources.getParentId()==null||tResources.getParentId()==0L){
				
				List<TResources> tesList = new ArrayList<TResources>();
				
				for(TResources trResourcesSon : resList)
					if(trResourcesSon.getParentId()!=null&&tResources.getId()==trResourcesSon.getParentId())
						tesList.add(trResourcesSon);	
				
				resultMap.put(tResources,tesList);
			}
		}
		 
		return resultMap; 
	}

	@Override
	public List<TResources> findByParentId(Long parentId) {
		 return resourceDao.find("FROM TResources WHERE parentId = ?", new Object[]{parentId});
	}

	@Override
	public void listByDataTable(DataTablePageUtil<TResources> dtp) {
		 resourceDao.listByDataTable(dtp, TResources.class);
	}

	@Override
	public List<String> findButtonsByUserName(String accountName , String buttonGroup, String menuId) {
		
		if(StringUtils.isEmpty(buttonGroup)||StringUtils.isEmpty(accountName)){
			return null;
		}
		
		try{
			Long.parseLong(menuId);
		}catch(Exception e){
			return null;
		}
		
		Set<TResources> trResources = userService.findResources(accountName , Long.parseLong(menuId));
		
		List<String> result = new ArrayList<String>();
		
		for(TResources resources : trResources){
				
			if(!StringUtils.isEmpty(resources.getButtonGroup()) 
					&& resources.getButtonGroup().equals(buttonGroup.trim())){
				
				TButton button=buttonService.findButtonBybid(resources.getBid());
				
				if(button!=null){
					String buttonStr = button.getButton();
					//防止嵌套替换产生未替换的项，首先替换#operate#
					buttonStr = CommonUtils.replaceAll(buttonStr,"#operate#", StringUtils.isEmpty(resources.getOperate())?"":resources.getOperate());
					buttonStr = CommonUtils.replaceAll(buttonStr,"#style#", StringUtils.isEmpty(resources.getStyle())?"":resources.getStyle());
					buttonStr = CommonUtils.replaceAll(buttonStr,"#name#", StringUtils.isEmpty(resources.getName())?"":resources.getName());
					buttonStr = CommonUtils.replaceAll(buttonStr,"#icon#", StringUtils.isEmpty(resources.getIcon())?"":resources.getIcon());
					buttonStr = CommonUtils.replaceAll(buttonStr,"#url#", 
							StringUtils.isEmpty(resources.getResUrl())?"":
								resources.getResUrl().startsWith("/")?
										resources.getResUrl().replaceFirst("/", ""):resources.getResUrl());
					result.add(buttonStr);
				}
			}
		}
	
		return result;
	}
	
	private void replace(String source , String regex ,String data){
		if(!StringUtils.isEmpty(source))
			source =  source.replaceAll(regex, data);
	}

	@Override
	public TResources getResourceById(Long id) {
		return resourceDao.get(TResources.class, id);
	}
	
}
